<?php
namespace app\member\controller;
class Weixinlogs extends Common{
    /**
     * 用于前端聊天组件获取聊天信息接口
     * @param  integer $soft_id   当前我的账号ID
     * @param  integer $social_id 好友ID
     * @return [type]             [description]
     */
    function logs($soft_id=0,$social_id=0,$offset=0,$append=false){
    	if(request()->isAjax()||request()->isPost()){
            $uid=$this->user['id'];
            $where=[
                'social_id'=>$social_id,
                'soft_id'=>$soft_id
            ];
            if(is_string($append)){
                $append=$append=='true';
            }
            if($append){
                $where['unread']=1;
            }
	        $list=model($this->table)->logs($where,$offset,$append);
            if($append&&!empty($list)){
                // 聊天组件 用户未读信息
                $unreadIds=model('weixinlogs')->unreadIds($uid);
                $data=[
                    'event'=>'wx_message',
                    'data'=>$unreadIds,
                ];
                \socket\Socket::admin($data,$uid);
                // 向移动端传递总的未读信息数
                // $counts=0;
                // foreach ($unreadIds as $v) {
                //     $counts+=$v;
                // }
                // $data=[
                //     'event'=>'wx_message_counts',
                //     'data'=>$counts,
                // ];
                //\socket\Socket::admin($data,$uid);
            }
	        $this->success('','',$list);
        }
    }
    /**
     * 取消未读
     * @return boolean [description]
     */
    function readed($social_id=0){
    	if(request()->isAjax()||request()->isPost()){
    		$where=[
    			'member_id'=>$this->user['id'],
    			'social_id'=>$social_id,
    			'unread'=>1,
    		];
            $list=model($this->table)->readAll($where,0,['order'=>'id']);
    		$edit=model($this->table)->edit(['unread'=>0],$where);
    		$this->success('','',$list);
    	}
    }
    /**
     * 发送信息消息
     */
    function add(){
    	if(request()->isAjax()||request()->isPost()){
    		$data=[
    			'dev_id'=>input('dev_id/d',0),
    			'soft_id'=>input('soft_id/d',0),
    			'social_id'=>input('social_id/d',0),
                'content'=>input('content/s',''),
                'type'=>input('type/d',0),
    			'media_id'=>input('media_id/d',0),
    		];
    		$validate=$this->validate($data,[
    			'dev_id|设备'=>'require',
    			'soft_id|账号'=>'require',
    			'social_id|好友'=>'require',
    			'content|内容'=>'require',
    		]);
    		if(true!==$validate){
    			$this->error($validate);
    		}
    		$uid=$this->user['id'];
    		$hasDev=model('device')->read(['id'=>$data['dev_id'],'member_id'=>$uid],'id,is_busy');
    		if(empty($hasDev)){
    			$this->error('设备不存在或已删除');
    		}
    		$hasSoft=model('software')->where(['id'=>$data['soft_id'],'member_id'=>$uid])->count();
    		if(!$hasSoft){
    			$this->error('账号不存在或已删除');
    		}
    		$hasSacial=model('social')->read(['id'=>$data['social_id'],'soft_id'=>$data['soft_id']],'id,type');
    		if(!$hasSacial){
    			$this->error('该账号不存在该好友');
    		}
    		$data['send_time']=time();
    		$data['member_id']=$uid;
    		$data['dev_id']=$data['dev_id'];
            $data['is_me']=1;
    		$data['send_status']=0;
    		$add=model($this->table)->add($data);
    		if($add){
                $rs=model($this->table)->createWxMsgTask($add,$hasSacial['type']);
                if($rs['success']){
                    $this->success('已加入发送队列中','',$add);
                    die;
                }
    		}
            $this->delMedia($data['media_id']);
    		$this->error('发送失败,请稍后再试');
    	}
    }
    /**
     * 聊天框 工具栏 处理接口
     * @param  string $cate 类型
     * @param  array  $data 数据
     * @return json
     */
    function toolbarEmit($cate=null,$data=[]){
        if(is_string($data)){
            $data=json_decode($data,true);
        }
        $uid=$this->user['id'];
        if($cate=='wxMsgInfos'){ //微信信息采集
            $model=model('device');
            foreach ($data as $v) {
                $model->isCreateWxMsgTask($uid,$v,true);
            }
            $this->success('创建采集队列任务成功');
            die();
        }elseif($cate=='readAll'){
            $where=[
                'member_id'=>$uid,
                'unread'=>1,
            ];
            $edit=model($this->table)->edit(['unread'=>0],$where);
            $data=[
                'event'=>'wx_message',
                'data'=>[],
            ];
            \socket\Socket::admin($data,$uid);
            $this->success('操作成功');
            die();
        }
    }
}
